package com.xingchi.tornado.dict.enums;

import com.xingchi.tornado.anno.Dict;
import com.xingchi.tornado.basic.BaseEnum;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 字典数据存储类型
 *
 * @author xingchi
 * @date 2024/5/19 21:07
 */
@Getter
@Dict(value = "DICT_STORAGE_TYPE", name = "字典存储类型", desc = "字典存储类型枚举")
public enum DictStorageTypeEnum implements BaseEnum {

    /**
     * 字典数据存储类型
     */
    IN_MEMORY(1, "内存存储", true),
    JDBC(2, "数据库", true),
    REDIS(3, "redis存储", false),

    ;
    private final Integer code;
    private final String desc;
    private final boolean isVisible;

    private static final Map<Integer, DictStorageTypeEnum> CODE_MAPPINGS = Arrays.stream(DictStorageTypeEnum.values())
                    .collect(Collectors.toMap(DictStorageTypeEnum::getCode, Function.identity()));

    DictStorageTypeEnum(Integer code, String desc, boolean isVisible) {
        this.code = code;
        this.desc = desc;
        this.isVisible = isVisible;
    }

    public static DictStorageTypeEnum getInstance(Integer code) {
        return CODE_MAPPINGS.get(code);
    }

    public static DictStorageTypeEnum getInstanceOrDefault(Integer code, DictStorageTypeEnum defaultEnum) {
        return CODE_MAPPINGS.getOrDefault(code, defaultEnum);
    }

}
